﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;
using System.IO;

namespace emulib
{
    class MSSQLCEHandler : IMmoDatabase
    {
        SqlCeConnection sqlceConnection;
        public Boolean Connect(ServerInfo sinfo)
        {
            //seems like both sqlite and sqlce don't like the word database...
            WorkDirectory wd = new WorkDirectory(sinfo);
            String path = wd.GetFullPath(sinfo.dbInfo.sqldb);
            String conString = "data source=" + path + ";";
            if (!File.Exists(path))
            {
                using(SqlCeEngine engine = new SqlCeEngine(conString))
                {
                    engine.CreateDatabase();
                }
            }
            sqlceConnection = new SqlCeConnection(conString);

            try
            {
                sqlceConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine("Connection error: " + e.Message);
                return false;
            }
            return true;
        }
        public void Disconnect()
        {

        }
        public IDataRecord Select(String what, String from, String where = null, String orderBy = null, Boolean direction = false, Int32 limitStart = -1, Int32 limitMax = -1)
        {
            return new SqlCeCommand().ExecuteReader();
        }
    }
}
